package com.relatev.minecraft.TRMobAPI;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

public class TRConfigManager {

    public final File MobConfigFolder;
    public final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
            .registerTypeAdapter(ItemStack.class, new ItemStackJsonDeserializer())
            .registerTypeAdapter(TRAbility.class, new TRAbilityJsonSolver())
            .registerTypeAdapter(TRMob.class, new TRMobJsonSerializer())
            .create();
    public final File CoreConfigFile;
    public final YamlConfiguration CoreConfig;

    public TRConfigManager() {
        CoreConfigFile = new File(TRMobAPI.getAPI().getDataFolder(), "config.yml");
        if (CoreConfigFile.exists() == false) {
            CoreConfigFile.getParentFile().mkdirs();
            try {
                CoreConfigFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(TRConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        CoreConfig = YamlConfiguration.loadConfiguration(CoreConfigFile);
        if (CoreConfig.getInt("Version") != Versioning.Version) {
            CoreConfig.set("Version", Versioning.Version);
            CoreConfig.set("AutoUpdate", true);
            try {
                CoreConfig.save(CoreConfigFile);
            } catch (IOException ex) {
                Logger.getLogger(TRConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        MobConfigFolder = new File(TRMobAPI.getAPI().getDataFolder(), "MobConfig");
        MobConfigFolder.mkdirs();
    }

    /**
     * 获取指定怪物的配置文件 获取时将自动创建配置文件
     *
     * @param tm 要获得的怪物类
     * @return 配置文件File
     */
    public File getTRMobConfigFile(TRMob tm) {
        File configfile = new File(MobConfigFolder, tm.ID + ".yml");
        if (configfile.exists() == false) {
            try {
                configfile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(TRConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return configfile;
    }

    /**
     * 获取指定怪物的配置文件中的TRJsonConfig内容 得到的Json字符串沿袭自TRJsonConfigurable
     *
     * @param tm 要获得的生物类
     * @return Json from TRJsonConfigurable
     */
    public String getTRMobConfigJson(TRMob tm) {
        File trmf = getTRMobConfigFile(tm);
        YamlConfiguration yaml = YamlConfiguration.loadConfiguration(trmf);
        if (yaml.contains("TRJsonConfig") == false) {
            yaml.set("TRJsonConfig", gson.toJson(tm, TRMob.class));
            try {
                yaml.save(trmf);
            } catch (IOException ex) {
                Logger.getLogger(TRConfigManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return yaml.getString("TRJsonConfig");
    }

    /**
     * 将此怪物类的所有可配置数据覆盖到已有配置文件中
     *
     * @param tm 要保存的生物类
     */
    public void saveTRMobConfigJson(TRMob tm) {
        File trmf = getTRMobConfigFile(tm);
        YamlConfiguration yaml = YamlConfiguration.loadConfiguration(trmf);
        yaml.set("TRJsonConfig", gson.toJson(tm, TRMob.class));
        try {
            yaml.save(trmf);
        } catch (IOException ex) {
            Logger.getLogger(TRConfigManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 从配置文件获取一个TRMob，注意，这绝非完整的生物类，仅有配置文件中的内容 如果没有什么特殊需求，你不应该调用这个方法
     *
     * @param tm 要获取的怪物类
     * @return 从配置文件中读取的残缺的怪物类
     */
    public TRMob getTRMobByConfig(TRMob tm) {
        return gson.fromJson(TRMobAPI.getAPI().getTRConfigManager().getTRMobConfigJson(tm), tm.getClass());
    }
}
